package com.yan.design.two.structure.s4;

import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 4. 组合模式
 *     目录类
 * @author shengren.yan
 * @create 2022-07-27
 */
@ToString
public class Directory extends Component{

    List<Component> list = new ArrayList<>();

    public Directory(String name,String age){
        super(name,age);
    }

    @Override
    public void print(){
        System.out.println("--------" + getName());
        list.forEach(Component::print);
    }

    @Override
    public void add(Component c) {
        list.add(c);
    }

    @Override
    public void remove(Component c) {
        list.remove(c);
    }

    @Override
    Component getChild(int index) {
        return list.get(index);
    }

    @Override
    public void test() {
        // 调用所有子组件的test方法
        list.forEach(Component::test);
    }

    public String getName() {
        return super.getName();
    }
    public String getDes() {
        return super.getDes();
    }


}
